package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "客户端-购物车相关接口")
@RequestMapping(value = "/user/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /*添加菜品或套餐到购物车中方法*/
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增购物车商品")
    public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("新增购物车，参数为{}", shoppingCartDTO.toString());
        // 调用service层方法
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    /*展示购物车所有商品方法*/
    @GetMapping(value = "/list")
    @ApiOperation(value = "查看购物车方法")
    public Result<List<ShoppingCart>> list() {
        log.info("查看购物车");
        // 调用service层方法
        List<ShoppingCart> shoppingCartList = shoppingCartService.showShoppingCart();
        return Result.success(shoppingCartList);
    }

    /*清空（删除所有）购物车*/
    @DeleteMapping(value = "/clean")
    @ApiOperation(value = "清空购物车方法")
    public Result<String> remove() {
        log.info("清空购物车方法");
        // 调用service层
        shoppingCartService.remove();
        return Result.success();
    }

    /*购物车商品数量减少（删除）方法*/
    @PostMapping(value = "/sub")
    @ApiOperation(value = "删除购物车一个商品方法")
    public Result<String> subtract(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("减少购物车商品方法，参数为{}", shoppingCartDTO.toString());
        // 调用service层方法
        shoppingCartService.subtract(shoppingCartDTO);
        return Result.success();
    }
}
